//
//  ImageHelper.m
//  ImageTest
//
//  Created by wei 王 on 11-11-12.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "ImageHelper.h"

NSString *documentFolder()
{
    return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
}

NSString *bundleFolder()
{
    return [[NSBundle mainBundle] bundlePath];
}

@implementation ImageHelper

+(UIImage *)imageFromUrlString:(NSString *)urlstring
{
    NSURL *url = [NSURL URLWithString:urlstring];
    return [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
}

+ (UIImage*)newImageNotCached:(NSString *)filename
{
    NSString *imageFile = [[NSString alloc] initWithFormat:@"%@/%@",
    [[NSBundle mainBundle] resourcePath], filename];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageFile];
    return image;
}

+(NSString *)pathForItemNamed:(NSString *)fname inFolder:(NSString *)path
{
    NSString *file;
    NSDirectoryEnumerator *entor = [[NSFileManager defaultManager] enumeratorAtPath:path];
    while (file = [entor nextObject]) {
        if([[file lastPathComponent] isEqualToString:fname])
            return [path stringByAppendingPathComponent:file];
    }
    return nil;
}

+ (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size
{  
    // 创建一个bitmap的context  
    // 并把它设置成为当前正在使用的context  
    UIGraphicsBeginImageContext(size);  
    // 绘制改变大小的图片  
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];  
    // 从当前context中创建一个改变大小后的图片  
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
    // 使当前的context出堆栈  
    UIGraphicsEndImageContext();  
    // 返回新的改变大小后的图片  
    return scaledImage;  
}

+ (UIImage *)scaleToSizeWithImageName:(NSString *)imageName size:(CGSize)size
{  
    UIImage *img = [ImageHelper imageWithName:imageName];
    // 创建一个bitmap的context  
    // 并把它设置成为当前正在使用的context  
    UIGraphicsBeginImageContext(size);  
    // 绘制改变大小的图片  
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];  
    // 从当前context中创建一个改变大小后的图片  
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
    // 使当前的context出堆栈  
    UIGraphicsEndImageContext();  
    // 返回新的改变大小后的图片  
    return scaledImage;  
}

+(UIImage *)createImageFromLayer:(CALayer *)layer
{
	UIGraphicsBeginImageContext(CGSizeMake(320, 480));
	[layer renderInContext:UIGraphicsGetCurrentContext()];
	UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return aImage;
}

+(UIImage *)createImageFromView:(UIView *)v
{
	UIGraphicsBeginImageContext(v.bounds.size);
	[v.layer renderInContext:UIGraphicsGetCurrentContext()];
	UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return aImage;
}

+(UIImage *)createImageFromView:(UIView *)v withRect:(CGRect)rect
{
	UIGraphicsBeginImageContext(v.bounds.size);
	[v.layer renderInContext:UIGraphicsGetCurrentContext()];
	UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	CGImageRef subImageRef = CGImageCreateWithImageInRect(aImage.CGImage, rect);   
//    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));   
//	
//    UIGraphicsBeginImageContext(smallBounds.size);   
//    CGContextRef context = UIGraphicsGetCurrentContext();   
//    CGContextDrawImage(context, smallBounds, subImageRef);   
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];   
//    UIGraphicsEndImageContext();   
	
    return smallImage;
}


@end
